package com.ufgov.smartclient.component.table;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.print.PageFormat;
import java.awt.print.Pageable;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * <p>Title: Smart Client(OSGI)</p>
 * <p>Description: 打印机，用于将打印结果打印为图象数组返回</p>
 * <p>Copyright: Copyright 2008 ufgov, Inc.</p>
 * <p>Company: ufgov</p>
 * <p>创建时间: 2008-3-5</p>
 * @author 刘永伟(manlge)
 * @version 1.0
 */
public final class PrinterJob extends java.awt.print.PrinterJob {
  private static PrinterJob printerJob = new PrinterJob(java.awt.print.PrinterJob
    .getPrinterJob());

  private java.awt.print.PrinterJob job;

  private transient PageFormat pageFormat;

  private transient Printable printable;

  public static PrinterJob getInstance() {
    return printerJob;
  }

  private PrinterJob(java.awt.print.PrinterJob job) {
    this.job = job;
    pageFormat = job.defaultPage();
  }

  @Override
  public void cancel() {
    job.cancel();
  }

  @Override
  public PageFormat defaultPage(PageFormat page) {
    return job.defaultPage(page);
  }

  @Override
  public int getCopies() {
    return job.getCopies();
  }

  @Override
  public String getJobName() {
    return job.getJobName();
  }

  @Override
  public String getUserName() {
    return job.getUserName();
  }

  @Override
  public boolean isCancelled() {
    return job.isCancelled();
  }

  @Override
  public PageFormat pageDialog(PageFormat page) throws HeadlessException {
    return job.pageDialog(page);
  }

  public PageFormat pageDialog() throws HeadlessException {
    pageFormat = job.pageDialog(pageFormat);
    return pageFormat;
  }

  @Override
  public void print() throws PrinterException {
    job.print();
  }

  @Override
  public boolean printDialog() throws HeadlessException {
    return job.printDialog();
  }

  @Override
  public void setCopies(int copies) {
    job.setCopies(copies);
  }

  @Override
  public void setJobName(String jobName) {
    job.setJobName(jobName);
  }

  @Override
  public void setPageable(Pageable document) throws NullPointerException {
    job.setPageable(document);
  }

  @Override
  public void setPrintable(Printable painter) {
    job.setPrintable(painter, pageFormat);
    this.printable = painter;
  }

  @Override
  public PageFormat validatePage(PageFormat page) {
    return job.validatePage(page);
  }

  @Override
  public void setPrintable(Printable painter, PageFormat format) {
    job.setPrintable(painter, format);
    this.printable = painter;
  }

  public List<String> printAsImages() throws PrinterException {
    List<String> pages = new ArrayList<String>();
    PageFormat pageFormat = getPageFormat();
    int pageWidth = (int) pageFormat.getWidth();
    int pageHeight = (int) pageFormat.getHeight();
    for (int index = 0;; index++) {
      BufferedImage image = new BufferedImage(pageWidth, pageHeight,
        BufferedImage.TYPE_INT_RGB);
      Graphics2D g2d = image.createGraphics();
      //      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
      //      g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
      Rectangle clip = new Rectangle(0, 0, pageWidth, pageHeight);
      g2d.setClip(clip);
      g2d.fill(clip);
      g2d.setPaint(Color.BLACK);
      clip.height--;
      clip.width--;
      g2d.draw(clip);
      if (printable.print(g2d, pageFormat, index) != Printable.PAGE_EXISTS)
        break;
      try {
        File file = File.createTempFile("printfile" + index, ".tmp");
        ImageIO.write(image, "PNG", file);
        pages.add(file.getAbsolutePath());
      } catch (IOException e) {
        throw new PrinterException(e.getMessage());
      }

      g2d.dispose();
    }
    return pages;
  }

  public PageFormat getPageFormat() {
    return pageFormat;
  }
}
